/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newToFormalUI.java
 *
 * Created on 2011-5-7, 17:57:18
 */
package junchangmis.student;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import publicmethod.*;
import junchangmis.*;
import junchangmis.codecSetting.*;
import javax.swing.*;
import java.util.Vector;
import java.util.StringTokenizer;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Point;
import java.sql.ResultSet;

/**
 *
 * @author developers
 */
public class newToFormalUI extends javax.swing.JFrame implements WindowListener {

    DefaultListModel newListModel = new DefaultListModel();
    DefaultListModel formalListModel = new DefaultListModel();
    Vector<newComer> newcomer = new Vector<newComer>();
    Vector<formalStu> formalstu = new Vector<formalStu>();
    String classCode;
    MainFrame parent;
    tipDialog td = new tipDialog(this, false);

    /** Creates new form newToFormalUI */
    public newToFormalUI(MainFrame parent) {
        /*   for (int i = 0; i < 50; i++) {
        newListModel.addElement(i);
        }*/
        this.parent = parent;
        initComponents();
        this.jListNew.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.jListFormal.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        this.jListNew.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                if (jListNew.getSelectedIndex() != -1) {
                    if (e.getClickCount() == 1) {
                        oneNewClick(jListNew.getSelectedValue());
                    }
                    if (e.getClickCount() == 2) {
                        twoNewClick(jListNew.getSelectedValue());
                    }
                }
            }
        });
        this.jListFormal.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                if (jListFormal.getSelectedIndex() != -1) {
                    if (e.getClickCount() == 1) {
                        oneFormalClick(jListFormal.getSelectedValue());
                    }
                    if (e.getClickCount() == 2) {
                        twoFormalClick(jListFormal.getSelectedValue());
                    }
                }
            }
        });

        this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        this.addWindowListener(this);
        //this.setLocationRelativeTo(null);
        tipDialog();
        this.setTitle("试听生转正");
    }

    void oneNewClick(Object value) {
        //  System.out.println("单击");
    }

    void twoNewClick(Object value) {
        System.out.println("试听生双击");
        //提取主键
        StringTokenizer st = new StringTokenizer(this.jListNew.getSelectedValue().toString());
        Vector<String> temp = new Vector<String>();
        while (st.hasMoreTokens()) {
            //System.out.println(st.nextToken());
            temp.add(st.nextToken());
        }
        String name = temp.get(0);
        String tel = temp.get(3);
        newComer newcomerTemp = newToFormalMethod.getSelectedNewComer(name, tel);
        if (newcomerTemp != null) {
            newComerInputUI fr = new newComerInputUI(this, newcomerTemp);
            this.setVisible(false);//隐藏自己
            fr.setVisible(true);//显示子窗口
        }
    }

    void tipDialog() {
        Point p = new Point(this.getX() + this.getWidth(), this.getY());
        td.setLocation(p);
        td.setVisible(true);
    }

    void oneFormalClick(Object value) {
        //  System.out.println("单击");
    }

    void twoFormalClick(Object value) {
        System.out.println("正式生双击");
        //提取主键
        StringTokenizer st = new StringTokenizer(this.jListFormal.getSelectedValue().toString());
        Vector<String> temp = new Vector<String>();
        while (st.hasMoreTokens()) {
            //System.out.println(st.nextToken());
            temp.add(st.nextToken());
        }
        String id = temp.get(0);
        System.out.println(id);
        formalStu student = new formalStu();
        stuChooseMethod.hasFormalStu(id, student);
        formalStuInputUI fr = new formalStuInputUI(this, true, this, student);
        //dispose(); //关闭自己
        fr.setVisible(true);//打开查询窗口

    }

    public void windowActivated(WindowEvent arg0) { //设置为活动窗口时触发
        //System.out.println("windowActivated -->窗口被选中。");
    }

    public void windowClosed(WindowEvent arg0) { //窗口被关闭时触发
        //System.out.println("windowClosed -->窗口被关闭。");
        //parent.setVisible(true);
    }

    public void windowClosing(WindowEvent arg0) { //窗口关闭时触发,按下关闭按钮
        System.out.println("windowClosing -->窗口关闭。");
        parent.setVisible(true);
    }

    public void windowDeactivated(WindowEvent arg0) { //设置为非活动窗口时触发
        //System.out.println("windowDeactivated -->取消窗口选中。");
    }

    public void windowDeiconified(WindowEvent arg0) { //窗口从最小化还原时触发
        //System.out.println("windowDeiconified -->窗口从最小化恢复。");
    }

    public void windowIconified(WindowEvent arg0) { //窗口最小化时触发
        //System.out.println("windowIconified -->窗口最小化。");
    }

    public void windowOpened(WindowEvent arg0) { //窗口最小化时触发
        //System.out.println("windowv -->窗口被打开。");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelLeft = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListNew = new javax.swing.JList();
        jPanelMiddle = new javax.swing.JPanel();
        jButtonToFormal = new javax.swing.JButton();
        jButtonToNew = new javax.swing.JButton();
        jPanelRight = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListFormal = new javax.swing.JList();
        jPanelBottom = new javax.swing.JPanel();
        jLabelClassCode = new javax.swing.JLabel();
        jTextFieldClassCode = new javax.swing.JTextField();
        jLabelCodeShow = new javax.swing.JLabel();
        jLabelLeft = new javax.swing.JLabel();
        jLabelRight = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        jPanelLeft.setLayout(new java.awt.GridLayout(1, 0));

        jListNew.setFont(new java.awt.Font("华文细黑", 0, 18));
        jListNew.setModel(this.newListModel);
        jScrollPane1.setViewportView(jListNew);

        jPanelLeft.add(jScrollPane1);

        jButtonToFormal.setFont(new java.awt.Font("华文细黑", 0, 18));
        jButtonToFormal.setText(">>");
        jButtonToFormal.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonToFormal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonToFormalActionPerformed(evt);
            }
        });

        jButtonToNew.setFont(new java.awt.Font("华文细黑", 0, 18));
        jButtonToNew.setText("<<");
        jButtonToNew.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonToNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonToNewActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelMiddleLayout = new javax.swing.GroupLayout(jPanelMiddle);
        jPanelMiddle.setLayout(jPanelMiddleLayout);
        jPanelMiddleLayout.setHorizontalGroup(
            jPanelMiddleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMiddleLayout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jPanelMiddleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonToFormal)
                    .addComponent(jButtonToNew))
                .addGap(37, 37, 37))
        );
        jPanelMiddleLayout.setVerticalGroup(
            jPanelMiddleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMiddleLayout.createSequentialGroup()
                .addGap(191, 191, 191)
                .addComponent(jButtonToFormal)
                .addGap(79, 79, 79)
                .addComponent(jButtonToNew))
        );

        jPanelRight.setLayout(new java.awt.GridLayout(1, 0));

        jListFormal.setFont(new java.awt.Font("华文细黑", 0, 18));
        jListFormal.setModel(this.formalListModel);
        jScrollPane2.setViewportView(jListFormal);

        jPanelRight.add(jScrollPane2);

        jLabelClassCode.setFont(new java.awt.Font("华文细黑", 0, 18));
        jLabelClassCode.setText("输入专业班级代码：");

        jTextFieldClassCode.setFont(new java.awt.Font("华文细黑", 0, 18)); // NOI18N
        jTextFieldClassCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldClassCodeActionPerformed(evt);
            }
        });
        jTextFieldClassCode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextFieldClassCodeKeyReleased(evt);
            }
        });

        jLabelCodeShow.setFont(new java.awt.Font("华文细黑", 0, 18));
        jLabelCodeShow.setText("--");

        javax.swing.GroupLayout jPanelBottomLayout = new javax.swing.GroupLayout(jPanelBottom);
        jPanelBottom.setLayout(jPanelBottomLayout);
        jPanelBottomLayout.setHorizontalGroup(
            jPanelBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBottomLayout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addComponent(jLabelClassCode)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldClassCode, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabelCodeShow, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(458, Short.MAX_VALUE))
        );
        jPanelBottomLayout.setVerticalGroup(
            jPanelBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBottomLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanelBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelClassCode)
                    .addComponent(jTextFieldClassCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCodeShow))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jLabelLeft.setFont(new java.awt.Font("华文细黑", 0, 18));
        jLabelLeft.setText("试听生");

        jLabelRight.setFont(new java.awt.Font("华文细黑", 0, 18));
        jLabelRight.setText("正式生");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelBottom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(143, 143, 143)
                                .addComponent(jLabelLeft))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jPanelLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanelMiddle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jPanelRight, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                            .addComponent(jLabelRight))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanelLeft, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                            .addComponent(jPanelRight, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelLeft, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabelRight, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addComponent(jPanelMiddle, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelBottom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonToFormalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonToFormalActionPerformed
        // TODO add your handling code here:
        if (this.jListNew.getSelectedIndex() != -1) {//将选中的试听生变为正式生
            formalStu student = new formalStu();
            //提取列表中选择的试听生的有用信息
            StringTokenizer st = new StringTokenizer(this.jListNew.getSelectedValue().toString());
            Vector<String> temp = new Vector<String>();
            while (st.hasMoreTokens()) {
                //System.out.println(st.nextToken());
                temp.add(st.nextToken());
            }
            student.name = temp.get(0);
            student.sex = temp.get(1);
            student.birthday = temp.get(2);
            student.tel = temp.get(3);
            student.school = temp.get(4);

            student.classCode = this.classCode;

            formalStuInputUI fr = new formalStuInputUI(this, true, student, this);
            fr.setVisible(true);

            //这个任务交给formalStuInputUI来做
            //this.formalListModel.addElement(fr.student.id + " " + this.jListNew.getSelectedValue());
            //this.newListModel.remove(this.jListNew.getSelectedIndex());
        }
    }//GEN-LAST:event_jButtonToFormalActionPerformed

    private void jButtonToNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonToNewActionPerformed
        // TODO add your handling code here:
        if (this.jListFormal.getSelectedIndex() != -1) {//将选中的正式生变为试听生
            newComer newcomerTemp = new newComer();
            //提取列表中选择的正式生的有用信息
            StringTokenizer st = new StringTokenizer(this.jListFormal.getSelectedValue().toString());
            Vector<String> temp = new Vector<String>();
            while (st.hasMoreTokens()) {
                //System.out.println(st.nextToken());
                temp.add(st.nextToken());
            }
            //由正式生转化为试听生
            newcomerTemp.classCode = temp.get(0).substring(0, 7);
            newcomerTemp.name = temp.get(1);
            newcomerTemp.sex = temp.get(2);
            newcomerTemp.birthday = temp.get(3);
            newcomerTemp.tel = temp.get(4);
            newcomerTemp.school = temp.get(5);
            if (newToFormalMethod.hasNewComer(newcomerTemp.name, newcomerTemp.tel, newcomerTemp.classCode) == false) {
                //添加试听生
                newComerInputMethod.addNewComer(newcomerTemp);//这时会重写该生的报名时间为当前系统时间
                //删除正式生
                formalStuInputMethod.deleteFormalStu(temp.get(0));

                //  rightToLeft();
                //通过库里的信息进行更新视图
                updateLeft();
                updateRight();
            } else {
                JOptionPane.showConfirmDialog(null, "该试听生已存在！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
            }
        }
    }//GEN-LAST:event_jButtonToNewActionPerformed

    void updateLeft() {
        //将试听生显示在左侧列表中
        this.newListModel.removeAllElements();
        newToFormalMethod.getNewComer(classCode, newcomer);
        for (int i = 0; i < newcomer.size(); i++) {
            this.newListModel.addElement(newcomer.get(i).name + " "
                    + newcomer.get(i).sex + " "
                    + newcomer.get(i).birthday + " "
                    + newcomer.get(i).tel + " "
                    + newcomer.get(i).school);
            // System.out.println(newcomer.get(i).name + " " + newcomer.get(i).tel + " " + newcomer.get(i).school);
            this.jListNew.updateUI();
        }
    }

    void updateRight() {
        //将正式生显示在右侧列表中
        this.formalListModel.removeAllElements();
        newToFormalMethod.getFormalStu(classCode, formalstu);
        for (int i = 0; i < formalstu.size(); i++) {
            this.formalListModel.addElement(formalstu.get(i).id + " "
                    + formalstu.get(i).name + " "
                    + formalstu.get(i).sex + " "
                    + formalstu.get(i).birthday + " "
                    + formalstu.get(i).tel + " "
                    + formalstu.get(i).school);
            this.jListFormal.updateUI();
        }
    }

    /*    void leftToRight(String id) {//左边一项移到右边
    this.formalListModel.addElement(id + " " + this.jListNew.getSelectedValue());
    this.newListModel.remove(this.jListNew.getSelectedIndex());
    }
    
    void rightToLeft() {
    int start = this.jListFormal.getSelectedValue().toString().indexOf(" ");
    this.newListModel.addElement(this.jListFormal.getSelectedValue().toString().substring(start + 1));
    this.formalListModel.remove(this.jListFormal.getSelectedIndex());
    }*/
    private void jTextFieldClassCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldClassCodeActionPerformed
        // TODO add your handling code here:
        classCode = this.jTextFieldClassCode.getText();
        if (classCode == null || classCode.length() != 7) {
            JOptionPane.showConfirmDialog(null, "专业班级的编码格式错误！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
            this.jLabelCodeShow.setText("");
            this.newListModel.removeAllElements();
            this.formalListModel.removeAllElements();
            this.jTextFieldClassCode.setSelectionStart(0);
        } else {
            String codeTriple = codecMethodNew.getMeaning(this.jTextFieldClassCode.getText());//获得总编码的翻译结果
            if (codeTriple == null) {
                JOptionPane.showConfirmDialog(null, "不存在该专业班级编码！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
                this.newListModel.removeAllElements();
                this.formalListModel.removeAllElements();
                this.jTextFieldClassCode.setSelectionStart(0);
                return;
            }
            this.jLabelCodeShow.setText("：" + codeTriple);
            //将试听生显示在左侧列表中
            updateLeft();
            //将正式生显示在右侧列表中
            updateRight();

        }
    }//GEN-LAST:event_jTextFieldClassCodeActionPerformed

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        // TODO add your handling code here:
        tipDialog();
    }//GEN-LAST:event_formMouseReleased
    boolean isBlank(String str) {
        if (str == null || str.trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }
    private void jTextFieldClassCodeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldClassCodeKeyReleased
        // TODO add your handling code here:
        if (isBlank(this.jTextFieldClassCode.getText())) {
            td.getItem(null);
        } else {
            try {
                // TODO add your handling code here:
                Vector<String> codes = new Vector<String>();
                DbOperation db = new DbOperation();
                db.DbConnect();
                String sql = "select * from codeNew";
                ResultSet rs = db.DBSqlQuery(sql);
                while (rs.next()) {
                    String code = rs.getString(1);
                    String meaning = rs.getString(2);
                    if (code.startsWith(this.jTextFieldClassCode.getText())) {
                        codes.add(code + "    " + meaning);
                    }
                }
                db.DbClose();
                td.getItem(codes);
            } catch (SQLException ex) {
                Logger.getLogger(newToFormalUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jTextFieldClassCodeKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*  java.awt.EventQueue.invokeLater(new Runnable() {
        
        public void run() {
        new newToFormalUI().setVisible(true);
        }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonToFormal;
    private javax.swing.JButton jButtonToNew;
    private javax.swing.JLabel jLabelClassCode;
    private javax.swing.JLabel jLabelCodeShow;
    private javax.swing.JLabel jLabelLeft;
    private javax.swing.JLabel jLabelRight;
    private javax.swing.JList jListFormal;
    private javax.swing.JList jListNew;
    private javax.swing.JPanel jPanelBottom;
    private javax.swing.JPanel jPanelLeft;
    private javax.swing.JPanel jPanelMiddle;
    private javax.swing.JPanel jPanelRight;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextFieldClassCode;
    // End of variables declaration//GEN-END:variables
}
